package com.lvmama.rhino.base;

import com.lvmama.entity.system.SysUser;
import com.lvmama.rhino.utils.RhinoConstant;
import redis.clients.jedis.Jedis;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;

public class SSOTokenUtil {
    public static SysUser getToken(HttpServletRequest request){
        String token = request.getParameter("token");
        if(null == token) {
            Cookie cookie = CookieUtil.getCookieByName(request, "token");
            token = cookie != null ? cookie.getValue() : null;
        }
        if(null == token){
            return null;
        }

        RhinoConstant rhinoConstant = RhinoConstant.getInstance();
        String JedisUrl = rhinoConstant.getValue("JedisUrl");
        String JedisPort = rhinoConstant.getValue("JedisPort");
        String microServiceId = rhinoConstant.getValue("microServiceId");
        Jedis jedis = new Jedis(JedisUrl, Integer.parseInt(JedisPort));
        byte[] bytes = token.getBytes();
        byte[] userBytes = jedis.get(bytes);
        SysUser currentUser = (SysUser) SerializeUtil.deSerialize(userBytes);
        if(null!=currentUser){
            currentUser.setRemarks(microServiceId);
        }
        jedis.close();
        return currentUser;
    }
}
